/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "buffer.h"

#include "memory.h"
#include "securec.h"

#include "iam_logger.h"

namespace OHOS {
namespace HDI {
namespace FaceAuth {
#define MAX_BUFFER_SIZE 512000
#define LOG_LABEL OHOS::UserIam::Common::LABEL_FACE_AUTH_IMPL

bool IsBufferValid(const Buffer *buffer)
{
    if ((buffer == NULL) || (buffer->buf == NULL) || (buffer->maxSize == 0) || (buffer->maxSize > MAX_BUFFER_SIZE) ||
        (buffer->contentSize > buffer->maxSize)) {
        return false;
    }

    return true;
}

bool CheckBufferWithSize(const Buffer *buffer, const uint32_t size)
{
    if ((!IsBufferValid(buffer)) || (buffer->contentSize != size)) {
        return false;
    }

    return true;
}

Buffer *CreateBufferBySize(const uint32_t size)
{
    if ((size == 0) || (size > MAX_BUFFER_SIZE)) {
        IAM_LOGE("Bad param size:%u", size);
        return NULL;
    }

    Buffer *buffer = (Buffer *)Malloc(sizeof(Buffer));
    if (buffer == NULL) {
        IAM_LOGE("Get buffer struct error");
        return NULL;
    }

    buffer->buf = (uint8_t *)Malloc(size);
    if (buffer->buf == NULL) {
        IAM_LOGE("Get buffer error");
        Free(buffer);
        return NULL;
    }

    if (memset_s(buffer->buf, size, 0, size) != EOK) {
        Free(buffer->buf);
        Free(buffer);
        return NULL;
    }
    buffer->maxSize = size;
    buffer->contentSize = 0;

    return buffer;
}

void DestoryBuffer(Buffer *buffer)
{
    if (buffer != NULL) {
        if (buffer->buf != NULL) {
            if (memset_s(buffer->buf, buffer->contentSize, 0, buffer->contentSize) != EOK) {
                IAM_LOGE("DestoryBuffer memset fail!");
            }
            Free(buffer->buf);
            buffer->buf = NULL;
            buffer->contentSize = 0;
            buffer->maxSize = 0;
        }
        Free(buffer);
    }
}

Buffer *CopyBuffer(const Buffer *buffer)
{
    if (!IsBufferValid(buffer)) {
        IAM_LOGE("Invalid buffer");
        return NULL;
    }

    Buffer *copyBuffer = CreateBufferBySize(buffer->maxSize);
    if (copyBuffer == NULL) {
        IAM_LOGE("Invalid buffer");
        return NULL;
    }

    if (memcpy_s(copyBuffer->buf, copyBuffer->maxSize, buffer->buf, buffer->contentSize) != EOK) {
        IAM_LOGE("Copy buffer fail");
        goto FAIL;
    }
    copyBuffer->contentSize = buffer->contentSize;

    return copyBuffer;

FAIL:
    DestoryBuffer(copyBuffer);

    return NULL;
}
} // namespace FaceAuth
} // namespace HDI
} // namespace OHOS